package com.example.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.DoubleBinaryOperator;

public class RedisDemoZSet {
    public static void test(Jedis jedis) {
        jedis.flushAll();
        System.out.println("zadd 和 zrange 使用");
        jedis.zadd("key",10.0,"zhangsan");
        Map<String ,Double> hash = new HashMap<>();
        hash.put("lisi",20.0);
        hash.put("wangwu",30.0);
        jedis.zadd("key",hash);
        List<String> members = jedis.zrange("key", 0, -1);
        System.out.println("members: "+members);
        List<Tuple> key = jedis.zrangeWithScores("key", 0, -1);
        System.out.println("key: " + key);

        System.out.println("zcard , zscore 使用");
        long len = jedis.zcard("key");
        System.out.println("len key: " +len);
        Double score  =  jedis.zscore("key","zhangsan");
        System.out.println("score: " + score);

        System.out.println("zrem , zrank 使用");
        Long zrank = jedis.zrank("key", "lisi");
        System.out.println("lisi rank: " + zrank);
        jedis.zrem("key","zhangsan");
        System.out.println("lisi rank: " + jedis.zrank("key", "lisi"));

    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            test(jedis);
        }
    }
}
